##############################################
#
# Will's Makefile
#
#############################################


# Switches
CROSSCOMPILE = y 

# Name of the binary
TARGET  := myapp

# Grabs a list of all relevant files
SRCS    := $(wildcard *.c)
OBJS    := ${SRCS:.c=.o}
DEPS    := ${SRCS:.c=.dep}

# Existing dep files
XDEPS   := $(wildcard ${DEPS})

# Target file system
INSTALLDIR := .

# Generic flags
CCFLAGS = -std=gnu99 -Os -Wall #-Werror
LDFLAGS =

STRIP = strip

# Cross compile?
ifeq ($(CROSSCOMPILE),y)

 # This is the directory containing all the toolchain etc..
 TOOLCHAIN_ROOT = /home/acra/CodeSourcery/Sourcery_G++_Lite/bin

 # Here are all the shared objects and headers for the target
 SYSROOT 		= /home/acra/ti-dvsdk_dm368-evm_4_02_00_06/br_sysroot

 PREFIX			= arm-none-linux-gnueabi-

 # The actual binaries
 CC 			= $(TOOLCHAIN_ROOT)/$(PREFIX)gcc
 LD 			= $(TOOLCHAIN_ROOT)/$(PREFIX)ld

 #Extra flags for cross compiling
 LDFLAGS 		+= --sysroot $(SYSROOT)
 CCFLAGS 		+= -I$(SYSROOT)/usr/include

else

 ${info Not cross compiling!}

endif

.PHONY: all clean distclean test

all: ${TARGET}

#ifneq (${XDEPS},)
include ${XDEPS}
#endif

$(TARGET): $(OBJS)
	@echo Generating binary [$@]...
	@${CC} ${LDFLAGS} ${LIBS} $^ -o $@

$(OBJS): %.o: %.c %.dep
	@echo Generating objs [$@]...
	@${CC} ${CCFLAGS} -o $@ -c $<

$(DEPS): %.dep: %.c Makefile
	@echo Generating Deps [$@] ...
	@${CC} ${CCFLAGS} -MM $< > $@

install: $(TARGET)
	@echo Installing [$@]....
	@sudo cp ${TARGET} ${INSTALLDIR}

clean:
	@-rm -f *~ *.o *.dep ${TARGET}

strip:
	@${STRIP} ${TARGET}

test:
	@echo Environment ----------------
	@echo CFLAGS: ${CCFLAGS}
	@echo LIBS: ${LIBS}
	@echo DEPS: ${DEPS}
	@echo XDEPS: ${XDEPS}
